package lt.inkredibl.iit;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JProgressBar;

import lt.inkredibl.iit.action.ActAutoCorners;
import lt.inkredibl.iit.action.ActClearContour;
import lt.inkredibl.iit.action.ActClearCorners;
import lt.inkredibl.iit.action.ActExit;
import lt.inkredibl.iit.action.ActLoad;
import lt.inkredibl.iit.action.ActOpenImg;
import lt.inkredibl.iit.action.ActPickCorners;
import lt.inkredibl.iit.action.ActSave;
import lt.inkredibl.iit.action.ActSetZoom;
import lt.inkredibl.iit.action.ActTransform;

@SuppressWarnings("serial")
public class FrmStraighten
	extends JFrame
{
	private static JProgressBar _pb;
	private ImgComponent _imgComp;

	protected FrmStraighten()
	{
		super("Straighten Tool");
	}

	public static FrmStraighten inst()
	{
		FrmStraighten frm = new FrmStraighten();
		frm.initContent();
		frm.setJMenuBar(makeMenuBar(frm));
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frm.pack();
		frm.setVisible(true);
		return frm;
	}

	private static JMenuBar makeMenuBar(FrmStraighten frame)
	{
		JMenuBar mb = new JMenuBar();
		JMenu file = new JMenu("File");
		mb.add(file);
		ImgComponent ic = frame.makeImageComponent();
		file.add(ActOpenImg.inst(ic));
		file.addSeparator();
		file.add(ActSave.inst(ic));
		file.add(ActLoad.inst(ic));
		file.add(ActClearContour.inst(ic));
		file.addSeparator();
		file.add(ActExit.inst());
		JMenu view = new JMenu("View");
		mb.add(view);
		view.add(ActSetZoom.inst(ic, 1));
		view.add(ActSetZoom.inst(ic, 2));
		view.add(ActSetZoom.inst(ic, 4));
		view.add(ActSetZoom.inst(ic, 8));
		view.add(ActSetZoom.inst(ic, 16));
		view.add(ActSetZoom.inst(ic, -2));
		view.add(ActSetZoom.inst(ic, -4));
		JMenu edit = new JMenu("Edit");
		mb.add(edit);
		edit.add(ActAutoCorners.inst(ic));
		edit.add(ActPickCorners.inst(ic));
		edit.add(ActClearCorners.inst(ic));
		edit.addSeparator();
		edit.add(ActTransform.inst(ic, makeProgressBar()));
		JMenu help = new JMenu("Help");
		mb.add(help);
		return mb;
	}

	private static JProgressBar makeProgressBar()
	{
		if (_pb == null)
		{
			_pb = new JProgressBar(JProgressBar.HORIZONTAL);
			_pb.setVisible(false);
		}
		return _pb;
	}

	private void initContent()
	{
		Container cp = getContentPane();
		cp.setLayout(new BorderLayout());
		cp.add(makeImageComponent().makeScroll());
		cp.add(makeProgressBar(), BorderLayout.SOUTH);
	}

	public ImgComponent makeImageComponent()
	{
		if (_imgComp == null)
		{
			_imgComp = new ImgComponent();
			_imgComp.setPreferredSize(new Dimension(256, 256));
		}
		return _imgComp;
	}

}
